//实现下滑
$.fn.slideDown = function (duration) {
    var height = this.height();

    this.css("visibility", "visible");
    this.css("height", 0);
    this.animate({
        height: height
    }, duration);
};

$(function(){
	FastClick.attach(document.body);//快速点击，消除移动端点击延迟的300ms时间
	/*去掉iphone手机滑动默认行为*/
	$('body').on('touchmove', function (event) {
	    event.preventDefault();
	});
	$("html, body").css("overflow", "hidden");
	
    //遮罩层
    $("#mask").slideDown("slow");
    $("#goBtn").on("click", function(){
        $("#mask").hide();
        $("html, body").css("overflow", "auto");
        $('body').off("touchmove");
    });
    
    //当音频已到达结尾时
    $("audio").on("ended", function(){
        this.pause();
        this.currentTime = 0;
        $(this).closest("li").find(".icon-pause").hide().prev().show();
    });
    
    //播放
    $(".icon-play").on("click", function(e){
        e.preventDefault();
        var audio = $(this).closest("li").find("audio")[0];

        $("audio").each(function(){
            this.pause();
            this.currentTime = 0;
            $(".icon-play").show();
            $(".icon-pause").hide();
        });
        $(this).hide().next().show();
        audio.play();
    }); 
        
    //暂停
    $(".icon-pause").on("click", function(e){
        e.preventDefault();
        var audio = $(this).closest("li").find("audio")[0];
        $(this).hide().prev().show();
        audio.pause();
    });

    //投票
    $(".songs-vote").on("click", function(e){
        e.preventDefault();

        var _this = $(this);
        var totalNum = parseInt(_this.closest(".songs-info").find(".songs-text__total").text());
        var voteId = _this.attr("voteId");
        var voterId= _this.attr("voterId");
        var voteSongId= _this.attr("voteSongId");
        
        $(".songs-vote").addClass("songs-voted");

        $.ajax({
          type: "get",
          url: "vote?voteId="+voteId+"&voterId="+voterId+"&voteSongId="+voteSongId,
          dataType: "json",
          success: function(data) {
                var returnFlag = data.returnFlag; //数据是否返回成功
                var code = data.code; //是否参与过报名
                
                if(code == "0"){
                    alert("感谢您的投票！");
                    _this.addClass("songs-voted").closest(".songs-info").find(".songs-text__total").text(++totalNum);
                }
                else{
                    alert("您已经投过票了！");
                }
            },
            error: function(msg) {
                alert("请求数据失败！");
            }
        });
    });
});